import json

# 读取API响应
with open('simple_api_response.json', 'r', encoding='utf-8') as f:
    response = json.load(f)

print("======= API响应格式验证结果 =======")
print(json.dumps(response, ensure_ascii=False, indent=2))
print("\n======= 详细验证结果 =======")

# 验证基本响应结构
basic_valid = True
if not all(key in response for key in ["code", "message", "data"]):
    basic_valid = False
    missing = set(["code", "message", "data"]) - set(response.keys())
    print(f"❌ 基本响应结构不符合Schema定义：缺少字段 {missing}")
else:
    print("✅ 基本响应结构符合Schema定义")
    print(f"   - code: {response['code']}")
    print(f"   - message: {response['message']}")

# 验证data结构
data = response.get("data", {})
pagination_valid = True
if "pagination" not in data:
    pagination_valid = False
    print("❌ data中缺少pagination字段")
else:
    print("✅ data包含pagination字段")
    
    pagination = data["pagination"]
    # 验证pagination结构
    if not all(key in pagination for key in ["total", "page", "page_size", "items"]):
        pagination_valid = False
        missing = set(["total", "page", "page_size", "items"]) - set(pagination.keys())
        print(f"❌ pagination结构不符合Schema定义：缺少字段 {missing}")
    else:
        print("✅ pagination结构符合Schema定义")
        print(f"   - total: {pagination['total']}")
        print(f"   - page: {pagination['page']}")
        print(f"   - page_size: {pagination['page_size']}")
        print(f"   - items数量: {len(pagination['items'])}")
        
        # 验证items中的项目对象结构
        items = pagination["items"]
        if items:
            project = items[0]
            expected_fields = ["id", "name", "description", "status", "created_at", "updated_at"]
            if all(key in project for key in expected_fields):
                print("✅ 项目对象结构符合Schema定义")
                print(f"   - id: {project['id']}")
                print(f"   - name: {project['name']}")
                print(f"   - description: {project['description']}")
                print(f"   - status: {project['status']}")
                print(f"   - created_at: {project['created_at']}")
                print(f"   - updated_at: {project['updated_at']}")
                
                # 验证status值是否为字符串类型
                if isinstance(project.get("status"), str):
                    print("✅ Status字段类型符合Schema定义(字符串类型)")
                else:
                    print("❌ Status字段类型不符合Schema定义")
                    print(f"   实际类型: {type(project.get('status'))}")
            else:
                print("❌ 项目对象结构不符合Schema定义")
                missing = set(expected_fields) - set(project.keys())
                print(f"   缺少字段: {missing}")
        else:
            print("⚠️ 项目列表为空，无法验证项目对象结构")

# 总体验证结果
print("\n======= 总体验证结果 =======")
if basic_valid and pagination_valid:
    print("✅ API返回格式完全符合Schema定义！")
    print("🎉 所有字段转换都已正确实现：")
    print("   - projectId -> id")
    print("   - projectName -> name")
    print("   - projectDescription -> description")
    print("   - createTime -> created_at")
    print("   - updateTime -> updated_at")
    print("   - status数值(1/0) -> 字符串('active'/'inactive')")
else:
    print("❌ API返回格式不符合Schema定义！")